package com.mlnx.ecg.ui.model;

import com.mlnx.ecg.ui.config.ECGDeviceConstant;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * The data structure to store patient's information
 *
 * @author jfeng
 */
@SuppressWarnings("serial")
public class ECGChannel implements Serializable {
    private boolean[] channelFlag;

    private boolean isOneMode;
    private int oneModeSelectChannel;

    /**
     * constructor
     */
    public ECGChannel() {
        channelFlag = new boolean[ECGDeviceConstant.ECGCHANNELNUM]; // 12
        // channel
        // flags
        channelFlag[1] = true;
    }

    public boolean isOneMode() {
        return isOneMode;
    }

    public void setIsOneMode(boolean isOneMode) {
        this.isOneMode = isOneMode;
    }

    public int getOneModeSelectChannel() {
        return oneModeSelectChannel;
    }

    public void setOneModeSelectChannel(int oneModeSelectChannel) {
        this.oneModeSelectChannel = oneModeSelectChannel;
    }

    public boolean getChannelFlag(int channel) {
        if (channel >= ECGDeviceConstant.ECGCHANNELNUM)
            return false;
        return channelFlag[channel];
    }

    public void setChannelFlag(int channel) {
        if (channel >= ECGDeviceConstant.ECGCHANNELNUM)
            return;
        channelFlag[channel] = true;
    }

    public int getSelectChannelNum() {
        int countChannel = 0;
        for (int i = 0; i < ECGDeviceConstant.ECGCHANNELNUM; ++i) {
            if (channelFlag[i])
                countChannel++;
        }
        return countChannel;
    }

    public int getSelectChannel(int selectIndex) {
        int temp = 0;
        for (int i = 0; i < ECGDeviceConstant.ECGCHANNELNUM; ++i) {
            if (channelFlag[i] && temp == selectIndex) {
                return i;
            } else if (channelFlag[i])
                temp++;
        }
        return -1;
    }

    public List<Integer> getSelectChannels() {
        List<Integer> channels = new ArrayList<Integer>();
        channels.add(1);
        for (int i = 0; i < ECGDeviceConstant.ECGCHANNELNUM; ++i) {
            if (channelFlag[i] && i != 1)
                channels.add(i);
        }
        return channels;
    }

    /**
     * turn off a channel (not read this channel)
     *
     * @param channel the index of channel need to turn off
     */
    public void cancelChannelFlag(int channel) {
        if (channel >= ECGDeviceConstant.ECGCHANNELNUM)
            return;
        channelFlag[channel] = false;
    }

    @Override
    public String toString() {
        return "ECGChannel{" + "channelFlag=" + Arrays.toString(channelFlag) + ", isOneMode=" + isOneMode
                + ", oneModeSelectChannel=" + oneModeSelectChannel + '}';
    }
}
